package com.bike.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

import org.springframework.http.HttpMethod;

public class SSLTrustManager implements javax.net.ssl.TrustManager,
            javax.net.ssl.X509TrustManager ,HostnameVerifier{
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
  
        public boolean isServerTrusted(
                java.security.cert.X509Certificate[] certs) {
            return true;
        }
  
        public boolean isClientTrusted(
                java.security.cert.X509Certificate[] certs) {
            return true;
        }
  
        public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }
  
        public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }
         
            @Override
        public boolean verify(String urlHostName, SSLSession session) { //允许所有主机
            return true;
        }
        
   //封装
public static HttpURLConnection connect(String strUrl) throws Exception {
         
         javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
         javax.net.ssl.TrustManager tm = new SSLTrustManager();
         trustAllCerts[0] = tm;
         javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
                 .getInstance("SSL");
         sc.init(null, trustAllCerts, null);
         javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
                 .getSocketFactory());
          
         HttpsURLConnection.setDefaultHostnameVerifier((HostnameVerifier) tm);
          
        URL url = new URL(strUrl);
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
      
        return urlConn;
    }


public static String request(String httpsUrl,String params,HttpMethod method){
	HttpURLConnection conn = null; //连接对象  
    InputStream is = null;  
    StringBuffer resultData = new StringBuffer("");  
    try {  
          
         
        conn = connect(httpsUrl); //使用URL打开一个链接  
         
        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        
        conn.setDoInput(true); //允许输入流，即允许下载  
        conn.setDoOutput(true); //允许输出流，即允许上传  
        conn.setUseCaches(false); //不使用缓冲  
        if(method==HttpMethod.GET){
        	conn.setRequestMethod("GET"); //使用get请求  
        }else{
        	conn.setRequestMethod("POST"); //使用POST请求  
        	byte[] bypes = params.toString().getBytes();
        	conn.getOutputStream().write(bypes);// 输入参数
        }
        is = conn.getInputStream();   //获取输入流，此时才真正建立链接  
        InputStreamReader isr = new InputStreamReader(is);  
        BufferedReader bufferReader = new BufferedReader(isr);  
        String inputLine  = "";  
        while((inputLine = bufferReader.readLine()) != null){  
            resultData .append( inputLine + "\n");  
        }  

    } catch (MalformedURLException e) {  
        e.printStackTrace();  
    }catch (IOException e) {  
        e.printStackTrace();  
    } catch (Exception e) {
		e.printStackTrace();
	}finally{  
        if(is != null){  
            try {  
                is.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        if(conn != null){  
            conn.disconnect();  
        }  
    }  

    return resultData.toString();  
}  

 public static void main(String[] args) throws Exception {
	 
//String httpsUrl="https://121.42.171.226/bike/forward/testparam";
//String param="sid=db3846f9321a05c9f47c47ebb886cb7b5b11250f1215199c32e2cdd892bb9dc2";
// String result=request(httpsUrl,param,HttpMethod.POST);
// System.out.println(result);
	 
	 String httpsUrl="https://121.42.171.226/bike/login/verificationcode";
	 String param="body={\"phonenumber\":\"13126506185\",\"auth\":\"8D401E83B8D745BADF81AEA3C3BCC941\"}";
	 String result=request(httpsUrl,param,HttpMethod.POST);
	 System.out.println(result);
	// System.out.println(Md5Util.MD5("18210405001"));
	 
//	 String httpsUrl="https://121.42.171.226/bike/login/login";
//	 String param="body={\"phonenumber\":\"13126506185\",\"verificationcode\":\"344311\"}";
//	 String result=request(httpsUrl,param,HttpMethod.POST);
	// System.out.println(6285896707L%69);
// 
// String httpsUrl="https://121.42.171.226/bike/bike/bikelist";
// String param="body={latitude:39.9070132025,longitude:116.3343507176,bikemodel:0}";
// String result=request(httpsUrl,param,HttpMethod.POST);
// System.out.println(result);
 
  
 
 
 
 
 
 
 
 
//	 String sid="db3846f9321a05c9f47c47ebb886cb7b5b11250f1215199c32e2cdd892bb9dc2";
//  String url="http://121.42.171.226/bike/bike/bikelist?body={\"phonenumber\":\"13126506185\",\"auth\":\"8D401E83B8D745BADF81AEA3C3BCC941\"}";
//	String result=OkHttpRequestUtil.doGet(url);
//System.out.println(result);
//String url="http://121.42.171.226/bike/bike/bikelist?body={\"bikemodel\":\"1\",\"longitude\":\"81.3123213\",\"latitude\":\"81.3123213\"}";
//String result=OkHttpRequestUtil.doGet(url);
//System.out.println(result);
//	 String sid="2705e99822731406cdea054571585d1890cf6bd582669fccb2de0d7f875d6ee4";
//		String channel="alipay";
//		String url="http://trade.icaipiao123.com/api/v3/trade/orders?plain=true&business=duobao&sid="+sid+"&channel="+channel+"&amount="+0.01;
//		String result=OkHttpRequestUtil.doGet(url);
//		System.out.println("\u8bf7\u5c1d\u8bd5\u9000\u51fa\u8d26\u6237\uff0c\u91cd\u65b0\u767b\u9646\u4e0b"+result);
//	 String sid="ece80ee55fd673ca5be3df48d4a059b508c9afe8fc60d1f7455a397aafa82ee0";
//		String url="http://trade.icaipiao123.com/api/v3/trade/status?plain=true&business=duobao&sid="+sid+"&orders_id=1374797";
//		String result=OkHttpRequestUtil.doGet(url);
//		System.out.println(result+"\u65e0orders\u4fe1\u606f");
//	 System.out.println("\u5145\u503c15\u5143");
}
 }